package cn.ymz.service.impl;

import cn.hutool.core.io.file.FileReader;
import cn.hutool.core.io.file.FileWriter;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import cn.ymz.model.Urls;
import cn.ymz.model.User;
import cn.ymz.service.LoginService;

import java.io.File;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class LoginServiceImpl implements LoginService {


    private final static LoginService INSTANCE = new LoginServiceImpl();

    private static final Log log = LogFactory.get();

    private LoginServiceImpl() {
    }

    public static LoginService getInstance() {
        return INSTANCE;
    }

    private Urls urls = Urls.getInstance();

    @Override
    public String login(User user) {
        String token = "";
        try {
            Map<String, Object> param = new HashMap<>();
            param.put("mobile", user.getMobile());
            param.put("password", user.getPassword());
            String body = HttpRequest.post(urls.getLoginUrl())
                    .form(param)
                    .execute()
                    .body();
            JSONObject res = JSONUtil.parseObj(body);
            if ("200".equals(res.getStr("code"))) {
                JSONObject data = res.getJSONObject("data");
                USER_MAP.put("userInfo", data);
                token = data.getStr("token");
                log.info("登录成功，token：" + token);
            } else {
                token = false + ":" + res.getStr("msg");
            }
        } catch (Exception ignored) {
        }
        return token;
    }

    @Override
    public List<String> getDomainUrl() {
        List<String> rslt = new ArrayList<>();
        Map<String, Object> param = new HashMap<>();
        String adminId = "61";
        try {
            File file = new File(System.getProperty("user.dir") + File.separator + "DLS.lmt");
            FileReader reader = new FileReader(file);
            adminId = reader.readString();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        param.put("admin_id", adminId);
        String body = HttpRequest.post(GET_DOMAIN_URL)
                .form(param)
                .execute()
                .body();
        JSONObject res = JSONUtil.parseObj(body);
        if ("200".equals(res.getStr("code"))) {
            JSONArray data = res.getJSONArray("data");
            for (int i = 0; i < data.size(); i++) {
                JSONObject jsonObject = data.getJSONObject(i);
                String domainName = jsonObject.getStr("domain_name");
                rslt.add(domainName);
            }
            if ("63".equals(adminId)) {
                if (!rslt.contains("saas010.ymz888.vip")) {
                    rslt.add("saas010.ymz888.vip");
                }
            }
        }

        return rslt;
    }


}
